package com.example.elysiumease.controller;



import com.example.elysiumease.exception.BusinessException;
import com.example.elysiumease.mapper.SinMapper;
import com.example.elysiumease.mapper.SoulMapper;
import com.example.elysiumease.mapper.SoulSinMapper;
import com.example.elysiumease.model.Sin;
import com.example.elysiumease.model.Soul;
import com.example.elysiumease.model.SoulSin; // 新增模型类导入
import com.example.elysiumease.model.StatusRecord;
import com.example.elysiumease.result.ResponseResult;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull; // 校验注解导入
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List; // 集合类导入


// 灵魂全生命周期管理
@RestController
@RequestMapping("/api/souls")
@Validated
@Slf4j
public class SoulController {

    //postman已经测试完成
    @Autowired
    private SoulMapper soulMapper;

    @Autowired
    private SoulSinMapper soulSinMapper;

    @Autowired
    private SinMapper sinMapper;

    @PostMapping
    public ResponseResult<Integer> createSoul(
            @Validated @RequestBody Soul soul) {
        if (soulMapper.insert(soul) <= 0) {
            throw new BusinessException("灵魂创建失败");
        }
        return ResponseResult.success(soul.getSoulId());
    }

    @GetMapping("/{id}")
    public ResponseResult<Soul> getSoul(
            @PathVariable @Min(1) Integer id) {
        log.info("[SoulController] id: {}", id);
        Soul soul = soulMapper.selectById(id);
        if (soul == null) {
            throw new BusinessException(404, "灵魂记录不存在");
        }
        return ResponseResult.success(soul);
    }

    @PutMapping("/{id}")
    public ResponseResult<Void> updateSoul(
            @PathVariable @Min(1) Integer id,
            @Validated @RequestBody Soul soul) {
        soul.setSoulId(id);
        if (soulMapper.update(soul) <= 0) {
            throw new BusinessException("灵魂信息更新失败");
        }
        return ResponseResult.success();
    }

    @DeleteMapping("/{id}")
    public ResponseResult<Void> deleteSoul(
            @PathVariable @Min(1) Integer id) {
        if (soulMapper.deleteById(id) <= 0) {
            throw new BusinessException("灵魂删除失败");
        }
        return ResponseResult.success();
    }

    @PostMapping("/{soulId}/sins")
    public ResponseResult<Void> addSoulSin(
            @PathVariable @Min(1) Integer soulId,
            @RequestParam @NotNull Short sinId,
            @RequestParam @Min(1) Integer occurrences) {

        // 1. 获取关联实体
        Soul soul = soulMapper.selectById(soulId);
        if (soul == null) {
            throw new BusinessException(404, "灵魂不存在");
        }

        Sin sin = sinMapper.selectById(sinId);
        if (sin == null) {
            throw new BusinessException(404, "罪行类型不存在");
        }

        // 2. 使用默认构造函数创建对象
        SoulSin soulSin = new SoulSin();
        soulSin.setSoul(soul);
        soulSin.setSin(sin);
        soulSin.setOccurrenceCount(occurrences.shortValue());  // 注意类型转换

        // 3. 保存记录
        if (soulSinMapper.insert(soulSin) <= 0) {
            throw new BusinessException("罪行添加失败");
        }

        return ResponseResult.success();
    }

    @GetMapping("/{soulId}/sins")
    public ResponseResult<List<SoulSin>> getSoulSins(
            @PathVariable @Min(1) Integer soulId) {
        return ResponseResult.success(soulSinMapper.selectBySoulId(soulId));
    }

    //状态追踪接口
    @GetMapping("/{id}/status-timeline")
    public ResponseResult<List<StatusRecord>> getStatusTimeline(
            @PathVariable @Min(1) Integer id) {
        return ResponseResult.success(soulMapper.selectStatusHistory(id));
    }
}
